package com.rolfhoward.video.record.ui

import android.content.Intent
import android.os.Environment
import android.widget.Toast
import com.qb.libcommon.activity.BaseActivity
import com.rolfhoward.video.R
import com.rolfhoward.video.record.manager.VideoEditManager
import kotlinx.android.synthetic.main.activity_video_service.*

/**
 *@author wuqiubin
 *@date 2022/9/15
 *@Describe 视频管理界面
 */
class VideoServiceActivity : BaseActivity() {

    override fun getLayoutId(): Int {
        return R.layout.activity_video_service
    }

    override fun initView() {
        lib_video_service_record.setOnClickListener {
            startActivity(
                Intent(
                    this,
                    VideoRecordActivity::class.java
                )
            )
        }
        lib_video_media_extractor.setOnClickListener { onMediaExtractor() }
        lib_video_media_muxer.setOnClickListener { onMediaMuxer() }
    }

    override fun initData() {

    }

    private fun onMediaExtractor() {
        Thread {
            VideoEditManager.getInstance()
                .onMediaExtractor(
                    this,
                    getExternalFilesDir(null)?.absolutePath + "/1_mp4abf6df24-b518-4607-aa07-3f1c0cacc983.mp4",
                    callback = { code: Int, message: String ->
                        runOnUiThread {
                            lib_video_media_extractor.setText(R.string.media_extractor)
                            lib_video_media_extractor.isEnabled = true
                            if (code == 0) {
                                lib_video_media_extractor.setText(R.string.doing_media_extractor)
                                lib_video_media_extractor.isEnabled = false
                            } else if (code == -1){
                                Toast.makeText(this, message, Toast.LENGTH_SHORT).show()
                            }
                        }
                    })
//            VideoEditManager.getInstance()
//                .onMediaExtractor(
//                    this,
//                    getExternalFilesDir(null)?.absolutePath + "/天地英雄.mp4",
//                    callback = { code: Int, message: String ->
//                        runOnUiThread {
//                            lib_video_media_extractor.setText(R.string.media_extractor)
//                            lib_video_media_extractor.isEnabled = true
//                            if (code == 3) {
//                                Toast.makeText(this, "视频解封装完成", Toast.LENGTH_SHORT).show()
//                            } else if (code == 0) {
//                                lib_video_media_extractor.setText(R.string.doing_media_extractor)
//                                lib_video_media_extractor.isEnabled = false
//                            } else {
//                                Toast.makeText(this, message, Toast.LENGTH_SHORT).show()
//                            }
//                        }
//                    })
        }.start()
    }

    private fun onMediaMuxer() {
        Thread {
            VideoEditManager.getInstance().onMediaMuxer(
                this,
                getExternalFilesDir(Environment.DIRECTORY_MUSIC)?.absolutePath + "/孤勇者.aac",
                getExternalFilesDir(Environment.DIRECTORY_MUSIC)?.absolutePath + "/天地英雄.mp4",
                callback = { code: Int, message: String ->
                    run {
                        runOnUiThread {
                            lib_video_media_muxer.setText(R.string.media_muxer)
                            lib_video_media_muxer.isEnabled = true
                            if (code == 3) {
                                Toast.makeText(this, "视频合并完成", Toast.LENGTH_SHORT).show()
                            } else if (code == 0) {
                                lib_video_media_muxer.setText(R.string.doing_media_muxer)
                                lib_video_media_muxer.isEnabled = false
                            } else {
                                Toast.makeText(this, message, Toast.LENGTH_SHORT).show()
                            }
                        }
                    }
                })
        }.start()
    }
}